#!/bin/bash

_PROG=nyyos

show_help() {
  echo -e "Kolla OpenStack OPs Tool"
  echo -e "Usage:"
  echo -e "\t$_PROG <osc|fip|gnochi|libvirt|carrier|sysctl> ..."
  echo -e "\nSubcommands:"
  echo -e "\tosc [...]                          run openstack command line\n"
  echo -e "\tfip [check|reboot|repair|rebuild]  check floating/fixed ip\n"
  echo -e "\tgnocchi <status|console|vm ...>    metric service\n"
  echo -e "\tlibvirt <check|clean>              check libvirt status or kill"
  echo -e "\t                                   stuck vms"
  echo -e "\tlibvirt prickvm zabbix-stat <uuid> install zabbix-stat in vm by qemu-ga\n"
  echo -e "\tcarrier [check]                    check network link carrier status"
  echo -e "\tsysctl [check]                     check kernel parameters"
  echo -e "\nExamples:"
  echo -e "\t$_PROG osc project list --domain Default"
  echo -e "\t$_PROG osc server list"
  echo -e "\t$_PROG fip check ALL"
  echo -e "\t$_PROG fip --project admin check 172.16.113.2"
  echo -e "\t$_PROG fip check 172.16.113.5:3389"
  echo -e "\t$_PROG libvirt check"
  echo -e "\nFiles:"
  echo -e "\t~/.nyyosrc               settings(See the nyyosrc(5) man page"
  echo -e "\t                         for more information)"
}

if [ $# -eq 0 ]; then
  show_help
  exit 0
fi

cmdopts=$(getopt --longoptions help \
                     --options +h -- "$@")

# set positional parameters
eval set -- "$cmdopts"

while true; do
  case "$1" in
    -h | --help )
        show_help
        exit ;;
    -- ) shift; break ;;
    * ) break ;;
  esac
done



. ~/.nyyosrc || exit -1

subcmd=$1
shift

if [[ ! "$subcmd" =~ osc|fip|libvirt|gnocchi|carrier|sysctl ]]; then
   echo "error input" >&1
   show_help >&1
   exit -1
fi

rundir=$(dirname $0)
cd $rundir

. do_$subcmd --title "$_PROG $subcmd" $*

